package com.xiaobawang.knowledge.spider.util;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * @author synhard
 * @description 随机获取UserAgent
 * @date 2024/3/28 21:26
 * @email songxinhang2023@e-chinalife.com
 * @id 18101119
 * @phone 13001321080
 */
public class HeaderUtil {
    private static final String[] USER_AGENT = {
            "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:85.0) Gecko/20100101 Firefox/85.0",
            "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3",
            "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36",
            "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36",
            "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0.3 Safari/605.1.15",
            "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0.2 Safari/605.1.15",
            "Mozilla/5.0 (Linux; Android 10; SM-A505FN) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.152 Mobile Safari/537.36",
            "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.705.63 Safari/537.36 Edg/88.0.705.63",
            "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Mobile Safari/537.36",
            "Mozilla/5.0 (iPhone; CPU iPhone OS 14_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0.3 Mobile/15E148 Safari/604.1"
    };

    private static final String ACCEPT = "*/*";
    private static final String ACCEPT_ENCODING = "gzip, deflate, br, zstd";
    private static final String ACCEPT_LANGUAGE = "zh-CN,zh;q=0.9";
    private static final String HOST = "www.nfra.gov.cn";
    private static final String REFERER = "https://" + HOST + "/cn/view/pages/ItemList.html";
    private static final String CONNECTION = "keep-alive";
    public static final String CHINALIFE_HOST = "www.e-chinalife.com";
    public static final String GOVERNMENT_HOST = "www.nfra.gov.cn";

    public static Map<String, Object> getHeader(String host, String referer) {
        Map<String, Object> header = new HashMap<>();
        header.put("Accept", ACCEPT);
        header.put("Accept-Encoding", ACCEPT_ENCODING);
        header.put("Accept-Language", ACCEPT_LANGUAGE);
        header.put("Connection", CONNECTION);
        header.put("Host", host);
        header.put("Referer", referer);
        header.put("User-Agent", USER_AGENT[new Random().nextInt(USER_AGENT.length)]);
        return header;
    }
}
